package com.yqg.protection.controller.dataHandler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSON;
import com.yqg.protection.entity.*;
import com.yqg.protection.service.*;
import com.yqg.protection.utils.DataSaver;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * projectName:xf-service
 * author:zxf
 * time:2025/8/30 17:09
 * description:
 */
@Component
public class DetectDataHandler {

    @Resource
    protected DataSaver dataSaver;

    @Resource
    private IEquipmentInputStoreService equipmentInputStoreService;

    @Resource
    private ICurrentDqService currentDqService;

    @Resource
    private IHistoryDqService historyDqService;

    @Resource
    private IWarnDqService warnDqService;

    @Resource
    private ICurrentJhService currentJhService;

    @Resource
    private IHistoryJhService historyJhService;

    @Resource
    private IWarnJhService warnJhService;

    @Resource
    private ICurrentLsService currentLsService;

    @Resource
    private IHistoryLsService historyLsService;

    @Resource
    private IWarnLsService warnLsService;

    @Resource
    private ICurrentQgService currentQgService;

    @Resource
    private IHistoryQgService historyQgService;

    @Resource
    private IWarnQgService warnQgService;

    @Resource
    private ICurrentYgService currentYgService;

    @Resource
    private IHistoryYgService historyYgService;

    @Resource
    private IWarnYgService warnYgService;

    @Resource
    private IWarnRecordService warnRecordService;

    @Resource
    ProtocolUtil protocolUtil;

    @Async("dataSavingThreadPool")
    public void saveDetectData(Map<String, Object> detectData) {
        Integer businesstype = (Integer) detectData.get("businesstype");
        String mac = (String) detectData.get("mac");
        EquipmentInputStore equipmentInputStore = protocolUtil.getEquipmentInputStore(mac);
        String sendTime;
        Long createtime;
        if (businesstype == 1 || businesstype == 4) {
            createtime = (Long) detectData.get("createtime");
            sendTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date(createtime));
        } else if (businesstype == 3) {
            createtime = (Long) detectData.get("onlinetime");
            sendTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date(createtime));
        } else {
            Date date = new Date();
            createtime = date.getTime();
            sendTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
        }
        LocalDateTime originalTime = LocalDateTimeUtil.of(createtime);
        LocalDateTime truncatedTime = truncateToSeconds(originalTime);
        // 2. 保存日志文件
        if (equipmentInputStore == null) {
            dataSaver.saveDataToFileAsync(JSON.toJSONString(detectData), sendTime, mac + "——设备未录入平台");
        } else {
            dataSaver.saveDataToFileAsync(JSON.toJSONString(detectData), sendTime, mac);
        }
        if (equipmentInputStore != null) {
            String explanation = "";
            Integer isonline = 1;
            if (businesstype == 1) {

                Object devicetypeO = detectData.get("devicetype");
                String devicetype;
                if (devicetypeO != null) {
                    devicetype = (String) devicetypeO;
                } else {
                    devicetypeO = detectData.get("codetype");
                    if (devicetypeO != null) {
                        devicetype = (String) devicetypeO;
                    } else {
                        devicetype = "";
                    }
                }

                Object imsi = detectData.get("imsi");
                if (imsi != null && !imsi.equals("") && (equipmentInputStore.getSimId() == null || equipmentInputStore.getSimId().equals(""))) {
                    equipmentInputStore.setSimId((String) imsi);
                }

                if (devicetype.equals("SY")) {
                    CurrentLs currentLs = new CurrentLs();
                    // 共用字段
                    currentLs.setEquipmentInputStoreId(equipmentInputStore.getId());
                    currentLs.setSendTime(truncatedTime);
                    currentLs.setBusinesstype(businesstype);
                    currentLs.setSmokeid((Integer) detectData.get("smokeid"));
                    currentLs.setMac(mac);
                    currentLs.setDevicetype((String) detectData.get("codetype"));
                    currentLs.setDsignal((Integer) detectData.get("dsignal"));
                    Object batterys = detectData.get("batterys");
                    if (batterys != null) {
                        currentLs.setBatterys((Integer) batterys);
                    }
                    Object volvalue = detectData.get("volvalue");
                    if (volvalue != null) {
                        currentLs.setVolvalue((Integer) volvalue);
                    }
                    Object voltagevalue = detectData.get("voltagevalue");
                    if (voltagevalue != null) {
                        currentLs.setVoltagevalue((Integer) voltagevalue);
                    }
                    if (imsi != null) {
                        currentLs.setImsi((String) imsi);
                    }

                    // 私有字段
                    Object hxmonitorid = detectData.get("hxmonitorid");
                    if (hxmonitorid != null) {
                        currentLs.setHxmonitorid((Integer) hxmonitorid);
                    }
                    Object type = detectData.get("type");
                    if (type != null) {
                        currentLs.setType((Integer) type);
                    }
                    Object code = detectData.get("code");
                    if (code != null) {
                        currentLs.setCode((String) code);
                    }
                    Object monitorname = detectData.get("monitorname");
                    if (monitorname != null) {
                        currentLs.setMonitorname((String) monitorname);
                    }
                    Object data = detectData.get("data");
                    if (data != null) {
                        currentLs.setData((String) data);
                    }
                    Object dataunit = detectData.get("dataunit");
                    if (dataunit != null) {
                        currentLs.setDataunit((String) dataunit);
                    }
                    Object collectiontime = detectData.get("collectiontime");
                    if (collectiontime == null) {
                        currentLs.setCollectiontime((Long) collectiontime);
                    }
                    String state = "0";
                    Object wpstatus = detectData.get("wpstatus");
                    if (wpstatus != null) {
                        currentLs.setWpstatus((Integer) wpstatus);
                        if ((Integer)wpstatus != 0) {
                            state = "1";
                        }
                    }
                    Object wrstatus = detectData.get("wrstatus");
                    if (wrstatus != null) {
                        currentLs.setWrstatus((Integer) wrstatus);
                        if ((Integer)wrstatus == 1) {
                            state = "1";
                        }
                    }
                    Object wdstatus = detectData.get("wdstatus");
                    if (wdstatus != null) {
                        currentLs.setWdstatus((Integer) wdstatus);
                        if ((Integer)wdstatus == 1) {
                            state = "1";
                        }
                    }
                    Object wkstatus = detectData.get("wkstatus");
                    if (wkstatus != null) {
                        currentLs.setWkstatus((Integer) wkstatus);
                        if ((Integer)wkstatus == 1) {
                            state = "1";
                        }
                    }
                    Object wostatus = detectData.get("wostatus");
                    if (wostatus != null) {
                        currentLs.setWostatus((Integer) wostatus);
                        if ((Integer)wostatus == 1) {
                            state = "1";
                        }
                    }
                    if (state.equals("0")) {
                        currentLs.setMessagetype(1);
                    } else {
                        currentLs.setMessagetype(2);
                    }
                    equipmentInputStore.setState(state);
                    Object singleflow = detectData.get("singleflow");
                    if (singleflow != null) {
                        currentLs.setSingleflow((String) singleflow);
                    }
                    Object totalflow = detectData.get("totalflow");
                    if (totalflow != null) {
                        currentLs.setTotalflow((String) totalflow);
                    }

                    if (!equipmentInputStore.getState().equals("0")) {
                        WarnLs warnLs = new WarnLs();
                        BeanUtil.copyProperties(currentLs, warnLs, true);
                        warnLsService.save(warnLs);
                    }
                    currentLsService.saveOrUpdate(currentLs);
                    HistoryLs historyLs = new HistoryLs();
                    BeanUtil.copyProperties(currentLs, historyLs, true);
                    historyLsService.save(historyLs);
                }
                if (devicetype.equals("DQ")) {
                    CurrentDq currentDq = new CurrentDq();
                    // 共用字段
                    currentDq.setEquipmentInputStoreId(equipmentInputStore.getId());
                    currentDq.setSendTime(truncatedTime);
                    currentDq.setBusinesstype(businesstype);
                    currentDq.setSmokeid((Integer) detectData.get("smokeid"));
                    currentDq.setMac(mac);
                    currentDq.setDevicetype((String) detectData.get("devicetype"));
                    currentDq.setDsignal((Integer) detectData.get("dsignal"));
                    Object batterys = detectData.get("batterys");
                    if (batterys != null) {
                        currentDq.setBatterys((Integer) batterys);
                    }
                    Object volvalue = detectData.get("volvalue");
                    if (volvalue != null) {
                        currentDq.setVolvalue((Integer) volvalue);
                    }
                    Object voltagevalue = detectData.get("voltagevalue");
                    if (voltagevalue != null) {
                        currentDq.setVoltagevalue((Integer) voltagevalue);
                    }
                    if (imsi != null) {
                        currentDq.setImsi((String) imsi);
                    }

                    // 私有字段
                    Object messagetypeO = detectData.get("messagetype");
                    if (messagetypeO != null) {
                        Integer messagetype = (Integer)messagetypeO;
                        if (messagetype == 2 || messagetype == 4 || messagetype == 6 || messagetype == 13 || messagetype == 15 || messagetype == 17) {
                            equipmentInputStore.setState("1");
                        } else if (messagetype == 9) {
                            equipmentInputStore.setState("2");
                        } else {
                            equipmentInputStore.setState("0");
                        }
                        currentDq.setMessagetype(messagetype);
                    }
                    Object uas = detectData.get("uas");
                    Object ua = detectData.get("ua");
                    Object ubs = detectData.get("ubs");
                    Object ub = detectData.get("ub");
                    Object ucs = detectData.get("ucs");
                    Object uc = detectData.get("uc");
                    Object cas = detectData.get("cas");
                    Object ca = detectData.get("ca");
                    Object cbs = detectData.get("cbs");
                    Object cb = detectData.get("cb");
                    Object ccs = detectData.get("ccs");
                    Object cc = detectData.get("cc");
                    Object leakvs = detectData.get("leakvs");
                    Object leakv = detectData.get("leakv");
                    Object tas = detectData.get("tas");
                    Object ta = detectData.get("ta");
                    Object tbs = detectData.get("tbs");
                    Object tb = detectData.get("tb");
                    Object tcs = detectData.get("tcs");
                    Object tc = detectData.get("tc");
                    Object tds = detectData.get("tds");
                    Object td = detectData.get("td");
                    Object tns = detectData.get("tns");
                    Object tn = detectData.get("tn");
                    Object pf = detectData.get("pf");
                    Object eqt = detectData.get("eqt");
                    Object p = detectData.get("p");
                    Object q = detectData.get("q");
                    Object qa = detectData.get("qa");
                    Object qb = detectData.get("qb");
                    Object qc = detectData.get("qc");
                    if (uas != null) {
                        currentDq.setUas((Integer) uas);
                    }
                    if (ua != null) {
                        currentDq.setUa((String) ua);
                    }
                    if (ubs != null) {
                        currentDq.setUbs((Integer) ubs);
                    }
                    if (ub != null) {
                        currentDq.setUb((String) ub);
                    }
                    if (ucs != null) {
                        currentDq.setUcs((Integer) ucs);
                    }
                    if (uc != null) {
                        currentDq.setUc((String) uc);
                    }
                    if (cas != null) {
                        currentDq.setCas((Integer) cas);
                    }
                    if (ca != null) {
                        currentDq.setCa((String) ca);
                    }
                    if (cbs != null) {
                        currentDq.setCbs((Integer) cbs);
                    }
                    if (cb != null) {
                        currentDq.setCb((String) cb);
                    }
                    if (ccs != null) {
                        currentDq.setCcs((Integer) ccs);
                    }
                    if (cc != null) {
                        currentDq.setCc((String) cc);
                    }
                    if (leakvs != null) {
                        currentDq.setLeakvs((Integer) leakvs);
                    }
                    if (leakv != null) {
                        currentDq.setLeakv((String) leakv);
                    }
                    if (tas != null) {
                        currentDq.setTas((Integer) tas);
                    }
                    if (ta != null) {
                        currentDq.setTa((String) ta);
                    }
                    if (tbs != null) {
                        currentDq.setTbs((Integer) tbs);
                    }
                    if (tb != null) {
                        currentDq.setTb((String) tb);
                    }
                    if (tcs != null) {
                        currentDq.setTcs((Integer) tcs);
                    }
                    if (tc != null) {
                        currentDq.setTc((String) tc);
                    }
                    if (tds != null) {
                        currentDq.setTbs((Integer) tds);
                    }
                    if (td != null) {
                        currentDq.setTd((String) td);
                    }
                    if (tns != null) {
                        currentDq.setTns((Integer) tns);
                    }
                    if (tn != null) {
                        currentDq.setTn((String) tn);
                    }
                    if (pf != null) {
                        currentDq.setPf((String) pf);
                    }
                    if (eqt != null) {
                        currentDq.setEqt((String) eqt);
                    }
                    if (p != null) {
                        currentDq.setP((String) p);
                    }
                    if (q != null) {
                        currentDq.setQ((String) q);
                    }
                    if (qa != null) {
                        currentDq.setQa((String) qa);
                    }
                    if (qb != null) {
                        currentDq.setQb((String) qb);
                    }
                    if (qc != null) {
                        currentDq.setQc((String) qc);
                    }

                    if (!equipmentInputStore.getState().equals("0")) {
                        WarnDq warnDq = new WarnDq();
                        BeanUtil.copyProperties(currentDq, warnDq, true);
                        warnDqService.save(warnDq);
                    }
                    currentDqService.saveOrUpdate(currentDq);
                    HistoryDq historyDq = new HistoryDq();
                    BeanUtil.copyProperties(currentDq, historyDq, true);
                    historyDqService.save(historyDq);
                }
                if (devicetype.equals("JH")) {
                    CurrentJh currentJh = new CurrentJh();
                    // 共用字段
                    currentJh.setEquipmentInputStoreId(equipmentInputStore.getId());
                    currentJh.setSendTime(truncatedTime);
                    currentJh.setBusinesstype(businesstype);
                    currentJh.setSmokeid((Integer) detectData.get("smokeid"));
                    currentJh.setMac(mac);
                    currentJh.setDevicetype((String) detectData.get("devicetype"));
                    currentJh.setDsignal((Integer) detectData.get("dsignal"));
                    Object batterys = detectData.get("batterys");
                    if (batterys != null) {
                        currentJh.setBatterys((Integer) batterys);
                    }
                    Object volvalue = detectData.get("volvalue");
                    if (volvalue != null) {
                        currentJh.setVolvalue((Integer) volvalue);
                    }
                    Object voltagevalue = detectData.get("voltagevalue");
                    if (voltagevalue != null) {
                        currentJh.setVoltagevalue((Integer) voltagevalue);
                    }
                    if (imsi != null) {
                        currentJh.setImsi((String) imsi);
                    }

                    // 私有字段
                    Object messagetypeO = detectData.get("messagetype");
                    if (messagetypeO != null) {
                        Integer messagetype = (Integer)messagetypeO;
                        if (messagetype == 2 || messagetype == 4 || messagetype == 6 || messagetype == 13 || messagetype == 15 || messagetype == 17) {
                            equipmentInputStore.setState("1");
                        } else if (messagetype == 9) {
                            equipmentInputStore.setState("2");
                        } else {
                            equipmentInputStore.setState("0");
                        }
                        currentJh.setMessagetype(messagetype);
                    }
                    Object ccid = detectData.get("ccid");
                    if (ccid != null) {
                        currentJh.setCcid((String) ccid);
                    }
                    Object imei = detectData.get("imei");
                    if (imei != null) {
                        currentJh.setImei((String) imei);
                    }
                    Object xdsmokelogid = detectData.get("xdsmokelogid");
                    if (xdsmokelogid != null) {
                        currentJh.setXdsmokelogid((Long) xdsmokelogid);
                    }
                    Object cstatus = detectData.get("cstatus");
                    if (cstatus != null) {
                        currentJh.setCstatus((Integer) cstatus);
                    }
                    if (!equipmentInputStore.getState().equals("0")) {
                        WarnJh warnJh = new WarnJh();
                        BeanUtil.copyProperties(currentJh, warnJh, true);
                        warnJhService.save(warnJh);
                    }

                    currentJhService.saveOrUpdate(currentJh);
                    HistoryJh historyJh = new HistoryJh();
                    BeanUtil.copyProperties(currentJh, historyJh, true);
                    historyJhService.save(historyJh);
                }
                if (devicetype.equals("QG")) {
                    CurrentQg currentQg = new CurrentQg();
                    // 共用字段
                    currentQg.setEquipmentInputStoreId(equipmentInputStore.getId());
                    currentQg.setSendTime(truncatedTime);
                    currentQg.setBusinesstype(businesstype);
                    currentQg.setSmokeid((Integer) detectData.get("smokeid"));
                    currentQg.setMac(mac);
                    currentQg.setDevicetype("QG");
                    currentQg.setDsignal((Integer) detectData.get("dsignal"));
                    Object batterys = detectData.get("batterys");
                    if (batterys != null) {
                        currentQg.setBatterys((Integer) batterys);
                    }
                    Object volvalue = detectData.get("volvalue");
                    if (volvalue != null) {
                        currentQg.setVolvalue((Integer) volvalue);
                    }
                    Object voltagevalue = detectData.get("voltagevalue");
                    if (voltagevalue != null) {
                        currentQg.setVoltagevalue((Integer) voltagevalue);
                    }
                    if (imsi != null) {
                        currentQg.setImsi((String) imsi);
                    }

                    // 私有字段
                    Object messagetypeO = detectData.get("messagetype");
                    if (messagetypeO != null) {
                        Integer messagetype = (Integer)messagetypeO;
                        if (messagetype == 2 || messagetype == 4 || messagetype == 6 || messagetype == 13 || messagetype == 15 || messagetype == 17) {
                            equipmentInputStore.setState("1");
                        } else if (messagetype == 9) {
                            equipmentInputStore.setState("2");
                        } else {
                            equipmentInputStore.setState("0");
                        }
                        currentQg.setMessagetype(messagetype);
                    }
                    if (!equipmentInputStore.getState().equals("0")) {
                        WarnQg warnQg = new WarnQg();
                        BeanUtil.copyProperties(currentQg, warnQg, true);
                        warnQgService.save(warnQg);
                    }

                    currentQgService.saveOrUpdate(currentQg);
                    HistoryQg historyQg = new HistoryQg();
                    BeanUtil.copyProperties(currentQg, historyQg, true);
                    historyQgService.save(historyQg);
                }
                if (devicetype.equals("YG")) {
                    CurrentYg currentYg = new CurrentYg();
                    // 共用字段
                    currentYg.setEquipmentInputStoreId(equipmentInputStore.getId());
                    currentYg.setSendTime(truncatedTime);
                    currentYg.setBusinesstype(businesstype);
                    currentYg.setSmokeid((Integer) detectData.get("smokeid"));
                    currentYg.setMac(mac);
                    currentYg.setDevicetype((String) detectData.get("devicetype"));
                    currentYg.setDsignal((Integer) detectData.get("dsignal"));
                    Object batterys = detectData.get("batterys");
                    if (batterys != null) {
                        currentYg.setBatterys((Integer) batterys);
                    }
                    Object volvalue = detectData.get("volvalue");
                    if (volvalue != null) {
                        currentYg.setVolvalue((Integer) volvalue);
                    }
                    Object voltagevalue = detectData.get("voltagevalue");
                    if (voltagevalue != null) {
                        currentYg.setVoltagevalue((Integer) voltagevalue);
                    }
                    if (imsi != null) {
                        currentYg.setImsi((String) imsi);
                    }

                    // 私有字段
                    Object messagetypeO = detectData.get("messagetype");
                    if (messagetypeO != null) {
                        Integer messagetype = (Integer)messagetypeO;
                        if (messagetype == 2 || messagetype == 4 || messagetype == 6 || messagetype == 13 || messagetype == 15 || messagetype == 17) {
                            equipmentInputStore.setState("1");
                        } else if (messagetype == 9) {
                            equipmentInputStore.setState("2");
                        } else {
                            equipmentInputStore.setState("0");
                        }
                        currentYg.setMessagetype(messagetype);
                    }
                    Object xdsmokelogid = detectData.get("xdsmokelogid");
                    if (xdsmokelogid != null) {
                        currentYg.setXdsmokelogid((Long) xdsmokelogid);
                    }
                    Object tvalue = detectData.get("tvalue");
                    if (tvalue != null) {
                        currentYg.setTvalue((String) tvalue);
                    }
                    Object cstatus = detectData.get("cstatus");
                    if (cstatus != null) {
                        currentYg.setCstatus((Integer) cstatus);
                    }
                    Object imei = detectData.get("imei");
                    if (imei != null) {
                        currentYg.setImei((String) imei);
                    }
                    if (!equipmentInputStore.getState().equals("0")) {
                        WarnYg warnYg = new WarnYg();
                        BeanUtil.copyProperties(currentYg, warnYg, true);
                        warnYgService.save(warnYg);
                    }

                    currentYgService.saveOrUpdate(currentYg);
                    HistoryYg historyYg = new HistoryYg();
                    BeanUtil.copyProperties(currentYg, historyYg, true);
                    historyYgService.save(historyYg);
                }
                explanation = "设备上报数据";

                // 保存报警数据
                if (!equipmentInputStore.getState().equals("0")) {
                    WarnRecord warnRecord = new WarnRecord();
                    warnRecord.setEquipmentInputStoreId(equipmentInputStore.getId());
                    warnRecord.setWarnStateNum(equipmentInputStore.getState());
                    warnRecord.setWarnTypeNum(0);
                    warnRecord.setIsDeal(0);
                    warnRecord.setIsMisinformation("0");
                    warnRecord.setTeleNotification("1");
                    warnRecord.setFaultReport("0");
                    warnRecord.setFieldSurvey("0");
                    warnRecord.setEliminHidDang("0");
                    warnRecord.setCreateTime(LocalDateTime.now());
                    warnRecord.setWarnTime(sendTime);
                    warnRecord.setWarnAddr(equipmentInputStore.getAddress());
                    warnRecord.setIsDelete(0);
                    warnRecordService.save(warnRecord);
                    //报警推送
                    ArrayList<WarnRecord> warnRecords = new ArrayList<>();
                    warnRecords.add(warnRecord);
                    protocolUtil.sendWarnNotice(equipmentInputStore, warnRecords);
                }
            } else if (businesstype == 3) {
                Object isonline1 = detectData.get("isonline");
                if (isonline1 != null) {
                    isonline = (Integer) isonline1;
                }
                explanation = "设备在线离线数据";
            } else if (businesstype == 4) {
                isonline = (Integer) detectData.get("isonline");
                Object dtypename = detectData.get("dtypename");
                String devicetype = (String) detectData.get("devicetype");
                Object dsignal = detectData.get("dsignal");
                Object siaddress = detectData.get("siaddress");
                Object sitelname = detectData.get("sitelname");
                Object installaddress = detectData.get("installaddress");
                Object longitude = detectData.get("longitude");
                Object latitude = detectData.get("latitude");
                CurrentDq currentDq = new CurrentDq();
                currentDq.setEquipmentInputStoreId(equipmentInputStore.getId());
                currentDq.setSendTime(truncatedTime);
                currentDq.setBusinesstype(businesstype);
                currentDq.setMac(mac);
                if (dtypename != null) {
                    currentDq.setDtypename((String) dtypename);
                }
                if (dsignal != null) {
                    currentDq.setDsignal((Integer) dsignal);
                }
                if (siaddress != null) {
                    currentDq.setSiaddress((String) siaddress);
                }
                if (sitelname != null) {
                    currentDq.setSitelname((String) sitelname);
                }
                if (installaddress != null) {
                    currentDq.setInstalladdress((String) installaddress);
                }
                if (longitude != null) {
                    currentDq.setLongitude((Long) longitude);
                }
                if (latitude != null) {
                    currentDq.setLatitude((Long) latitude);
                }
                if (devicetype.equals("DQ")) {
                    currentDqService.saveOrUpdate(currentDq);
                    HistoryDq historyDq = new HistoryDq();
                    BeanUtil.copyProperties(currentDq, historyDq, true);
                    historyDqService.save(historyDq);
                } else if (devicetype.equals("JH")) {
                    CurrentJh currentJh = new CurrentJh();
                    BeanUtil.copyProperties(currentDq, currentJh, true);
                    currentJhService.saveOrUpdate(currentJh);
                    HistoryJh historyJh = new HistoryJh();
                    BeanUtil.copyProperties(currentJh, historyJh, true);
                    historyJhService.save(historyJh);
                } else if (devicetype.equals("LS")) {
                    CurrentLs currentLs = new CurrentLs();
                    BeanUtil.copyProperties(currentDq, currentLs, true);
                    currentLsService.saveOrUpdate(currentLs);
                    HistoryLs historyLs = new HistoryLs();
                    BeanUtil.copyProperties(currentLs, historyLs, true);
                    historyLsService.save(historyLs);
                } else if (devicetype.equals("QG")) {
                    CurrentQg currentQg = new CurrentQg();
                    BeanUtil.copyProperties(currentDq, currentQg, true);
                    currentQgService.saveOrUpdate(currentQg);
                    HistoryQg historyQg = new HistoryQg();
                    BeanUtil.copyProperties(currentQg, historyQg, true);
                    historyQgService.save(historyQg);
                } else if (devicetype.equals("YG")) {
                    CurrentYg currentYg = new CurrentYg();
                    BeanUtil.copyProperties(currentDq, currentYg, true);
                    currentYgService.saveOrUpdate(currentYg);
                    HistoryYg historyYg = new HistoryYg();
                    BeanUtil.copyProperties(currentYg, historyYg, true);
                    historyYgService.save(historyYg);
                }
                if (dtypename != null && !dtypename.equals("") && (equipmentInputStore.getEquipmentModel() == null || equipmentInputStore.getEquipmentModel().equals(""))) {
                    equipmentInputStore.setEquipmentModel((String) dtypename);
                }
                if (installaddress != null && !installaddress.equals("") && (equipmentInputStore.getAddress() == null || equipmentInputStore.getAddress().equals(""))) {
                    equipmentInputStore.setAddress((String) installaddress);
                }
                explanation = "设备绑定事件（新增，修改）";
            } else {
                explanation = "未知数据类型";
            }
            equipmentInputStore.setUpdateTime(sendTime);
            equipmentInputStoreService.saveOrUpdate(equipmentInputStore);
            protocolUtil.saveFireSysData(mac, explanation, sendTime);
            if (isonline == 1) {
                protocolUtil.updateTemporaryData(mac, sendTime);
            }
        }
    }

    private LocalDateTime truncateToSeconds(LocalDateTime dateTime) {
        return dateTime.withNano(0);
    }
}
